<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$party                = array();
$partyBullion         = array();
$kaarigar             = array();
$bhatthiParty         = array();
$partyName            = array();
$rojmel               = array();
$creditAmount         = 0 ;
$debitAmount          = 0 ;
$creditFine           = 0 ;
$debitFine            = 0 ;
$rojmelId             = 0 ;
$vepaariPartyId       = 0 ;
$kaarigarPartyId      = 0 ;
$bullionPartyId       = 0 ;
$bhatthiPartyRojmelId	= 0 ;
$requiredVepaariId	  = 0 ;
$partyId             	= 0 ;
$totalFine            = 0 ;
$totalAmount          = 0 ;
$showPrev             = 0 ;
$billBookId           = 0 ;
$vepaariMasterId      = 0 ;
$particulars          = isset($_POST['particulars']) ? $_POST['particulars'] : 0;
$vepaariBookMasterId  = isset($_GET['vepaariBookMasterId']) ? $_GET['vepaariBookMasterId'] : "";

//Cancel:Start
if(isset($_POST['cancelBtn']))
{
    header("Location:rojmel.php");
    exit();
}
//Cancel:End

if(isset($_POST['party']))
{
  if($_POST['party'] != 0)
  {
    $partyPost = $_POST['party'];
    $fieldOfParty = "vepaariPartyId";
  }
  else if($_POST['kaarigar'] != 0)
  {
    $partyPost = $_POST['kaarigar'];
    $fieldOfParty = "kaarigarPartyId";
  }
  else if($_POST['bullionParty'] != 0)
  {
    $partyPost = $_POST['bullionParty'];
    $fieldOfParty = "bullionPartyId";
  }
  else if($_POST['bhatthiParty'] != 0)
  {
    $partyPost = $_POST['bhatthiParty'];
    $fieldOfParty = "bhatthiPartyRojmelId";
  }
  else
  {
    $partyPost = 0;
    $fieldOfParty = "vepaariPartyId";
  }
}
if(isset($partyPost))
{
	if($partyPost == 0)
	{
	  $partyId   = 0;
    $partyName = "";
  }
  else
	{
	  $partyNameAndId = explode("_", $partyPost);
	  $partyId   = $partyNameAndId[0];
    $partyName = $partyNameAndId[1];
  }
}
if(isset($_POST['submitBtn']))
{
  $fine   = ($_POST['fine'] != "") ? $_POST['fine'] : 0;
  $amount = ($_POST['amount'] != "") ? $_POST['amount'] : 0;

  if($_POST['party'] == 0 && $_POST['kaarigar'] == 0 && $_POST['bhatthiParty'] == 0 &&  $_POST['bullionParty'] == 0)
  {
    if(isset($_POST['rojmelId'])  && $_POST['rojmelId'] > 0)
    {
    	$updateRojmel = "UPDATE rojmel SET rojmelDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                              ".$fieldOfParty." = ".$partyId.", partyName = '".$partyName."', particulars = '".$_POST['particulars']."', fine = ".$fine.", 
                              amount = ".$amount.", CrDr = '".$_POST['crdr']."'
                        WHERE rojmelId = ".$_POST['rojmelId'];
      $updateRojmelRes = mysql_query($updateRojmel);
    }
    else
    {
     $insertRoamel = "INSERT INTO rojmel(rojmelDate,".$fieldOfParty.",partyName, particulars, fine, amount, CrDr)
                       VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',".$partyId.",'".$partyName."','".$_POST['particulars']."',
                               ".$fine.",".$amount.",'".$_POST['crdr']."')";
      $insertRoamelRes = mysql_query($insertRoamel);
    }
    header("Location:rojmel.php");
  }
  else if($_POST['party'] > 0)////////////// Vepaari Book Update and Insert Work : Starts////////////////////
  { 
    if(isset($_POST['rojmelId']) && $_POST['rojmelId']  > 0)
    {
    	// first all party bydefault id 0 set start
    	$updateAllDefaultParty = "UPDATE rojmel 
    	                             SET vepaariPartyId       = '0',
                                       kaarigarPartyId      = '0',
                                       bullionPartyId       = '0',
                                       bhatthiPartyRojmelId = '0'
                                 WHERE rojmelId = ".$_POST['rojmelId'];
      $updateAllDefaultPartyRes = mysql_query($updateAllDefaultParty);
      // first all party bydefault id 0 set End
      
      
      //Rojmel Updte : Starts
      $updateRojmel = "UPDATE rojmel
                           SET rojmelDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                           vepaariPartyId = ".$partyId.", partyName = '".$partyName."', particulars = '".$particulars."',fine = ".$fine.",
                           amount = ".$amount.", CrDr = '".$_POST['crdr']."'
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $updateRojmelRes = mysql_query($updateRojmel);
      //Rojmel Updte : Ends
      
      //Sellection of Main Id : Starts
      $selectIdQuery = "SELECT vepaariBookMasterId
                          FROM vepaaribilldetail
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $selectIdQueryRes = mysql_query($selectIdQuery);
      while($idRow = mysql_fetch_assoc($selectIdQueryRes)){
       $requiredVepaariId = $idRow['vepaariBookMasterId'];}                   
      //Sellection of Main Id : Ends

      //VepaariMaster Updte : Starts
      $updateVepaariMaster = "UPDATE vepaaribillmaster 
                                 SET partyId = ".$partyId.", vepaariBookDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."'
                               WHERE vepaariBookMasterId = ".$requiredVepaariId;
      $updateVepaariMasterRes = mysql_query($updateVepaariMaster);
      //VepaariMaster Updte : Ends
      
      /// Update Vepaari Detail : Starts
      $updateRojmelId = "SELECT rojmelId
                           FROM vepaaribilldetail 
                          WHERE vepaariBookMasterId = ".$requiredVepaariId;
      $updateRojmelIdRes = mysql_query($updateRojmelId);    
	    if($idRow = mysql_fetch_assoc($updateRojmelIdRes))
	    {
		    $deleteRojmelQuery = "UPDATE vepaaribilldetail 
                                 SET particulars = '".$_POST['particulars']."', fine = ".$_POST['fine'].",amount = ".$_POST['amount'].",CrDr = '".$_POST['crdr']."'
                               WHERE rojmelId = ".$idRow['rojmelId'];
		    $deleteRojmelQueryRes = mysql_query($deleteRojmelQuery);
	    }
      /// Update Vepaari Detail : Starts
      
      header("Location:rojmel.php");
      //VepaaruDetail Update : Ends
    }
    else
    {
      $particulars = (isset($_POST['particulars']))? $_POST['particulars'] : 0;    
      $fine        = (isset($_POST['fine']) && $_POST['fine'] > 0) ? $_POST['fine'] : 0;   
      $amount      = (isset($_POST['amount']) && $_POST['amount'] > 0) ? $_POST['amount'] : 0;   
       
      $insertRoamel = "INSERT INTO rojmel(rojmelDate,vepaariPartyId,kaarigarPartyId,bullionPartyId,bhatthiPartyRojmelId,partyName,particulars,fine, amount, CrDr)
                       VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',".$partyId.",0,0,0,'".$partyName."',
                               '".$particulars."',".$fine.",".$amount.",'".$_POST['crdr']."')";
      $insertRoamelRes = mysql_query($insertRoamel);
      $billBookId = mysql_insert_id();
     
      $billBookInsert = "INSERT INTO vepaaribillmaster (partyId,vepaariBookDate)
                         VALUES (".$partyId.",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."')";
      $billBookInsertRes = mysql_query($billBookInsert);
      $vepaariMasterId = mysql_insert_id();
       
     
      $insertPartyDetail = "INSERT INTO vepaaribillDetail (vepaariBookMasterId,particulars, fine, amount,CrDr,rojmelId,partyId)
                            VALUES (".$vepaariMasterId.",'".$particulars."','".$fine."','".$amount."','".$_POST['crdr']."',".$billBookId.",".$partyId.")";
      $insertPartyDetailRes = mysql_query($insertPartyDetail);
      header("Location:rojmel.php");
    }
  }////////////// Vepaari Book Update and Insert Work : Ends////////////////////
  else if($_POST['kaarigar'] > 0)////////////// Kaarigar Book Update and Insert Work : Starts////////////////////
  {
    if(isset($_POST['rojmelId'] ) && $_POST['rojmelId'] > 0)
    { 
      if($_POST['crdr'] == "Cr")
      {
        $field = "crAmount";
        $oppField = "drAmount";
      }  
      else
      {
        $field = "drAmount";
        $oppField = "crAmount";
      }
      
      // first all party bydefault id 0 set start
    	$updateAllDefaultParty = "UPDATE rojmel 
    	                             SET vepaariPartyId       = '0',
                                       kaarigarPartyId      = '0',
                                       bullionPartyId       = '0',
                                       bhatthiPartyRojmelId = '0'
                                 WHERE rojmelId = ".$_POST['rojmelId'];
      $updateAllDefaultPartyRes = mysql_query($updateAllDefaultParty);
      // first all party bydefault id 0 set End
      
      //Rojmel Updte : Starts
      $updateRojmel = "UPDATE rojmel
                           SET rojmelDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                               kaarigarPartyId = ".$partyId.",partyName = '".$partyName."',particulars = '".$particulars."',
                               fine = ".$fine.",amount = ".$amount.",CrDr = '".$_POST['crdr']."'
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $updateRojmelRes = mysql_query($updateRojmel);
      //Rojmel Updte : Ends

      //KaarigarMaster Update : Starts
      $updateKaarigarMaster = "UPDATE kaarigarmaster
                                  SET kaarigarId = ".$partyId.",
                                      kaarigarDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."'
                                WHERE rojmelId = ".$_POST['rojmelId'];
      $updateKaarigarMasterRes = mysql_query($updateKaarigarMaster);                          
      //KaarigarMaster Update : Ends
      
      //Sellection of Main Id : Starts
      $selectIdQuery = "SELECT kaarigarMasterId
                          FROM kaarigarmaster
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $selectIdQueryRes = mysql_query($selectIdQuery);
      while($kaarigarRow = mysql_fetch_assoc($selectIdQueryRes)){
       $requiredKaarigarId = $kaarigarRow['kaarigarMasterId'];}                   
      //Sellection of Main Id : Ends
      
      //KaarigarDetail Update : Starts
      $updateKaarigar = "UPDATE kaarigardetail 
                            SET particulars = '".$particulars."',".$field." = ".$amount.",".$oppField." = 0
                          WHERE kaarigarMasterId = ".$requiredKaarigarId;
      $updateKaarigarRes = mysql_query($updateKaarigar);
      header("Location:rojmel.php");
      //KaarigarDetail Update : Starts
    }
    else
    { 
      if($_POST['crdr'] == "Cr")
      {
        $field = "crAmount";
        $oppField = "drAmount";
      }
      else
      {
        $field = "drAmount";
        $oppField = "crAmount";
      }
      
      $insertRoamel = "INSERT INTO rojmel(rojmelDate,vepaariPartyId,kaarigarPartyId,bullionPartyId,bhatthiPartyRojmelId,partyName,particulars,fine,amount, CrDr)
                       VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',0,'".$partyId."',0,0,'".$partyName."','".$particulars."',
                               ".$fine.",".$amount.",'".$_POST['crdr']."')";
      $insertRoamelRes = mysql_query($insertRoamel);
      $requiredRojmelId = mysql_insert_id();
      
      $insertMasterkaarigar = "INSERT INTO  kaarigarmaster(kaarigarId,kaarigarDate,rojmelId)
                               VALUES ('".$partyId."','".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."','".$requiredRojmelId."')";
      $insertMasterkaarigarRes = mysql_query($insertMasterkaarigar);
      $masterId =mysql_insert_id();
      
      $insertKaarigar = "INSERT INTO kaarigardetail (kaarigarMasterId,particulars,".$field.",".$oppField.")
                         VALUES ('".$masterId."','".$particulars."',".$amount.",0)";
      $insertKaarigarRes = mysql_query($insertKaarigar);
      header("Location:rojmel.php");
    }
  }////////////// Kaarigar Book Update and Insert Work : Ends////////////////////
  else if($_POST['bhatthiParty'] > 0)////////////// Bhatthi Party Book Update and Insert Work : Starts////////////////////
  {
    if(isset($_POST['rojmelId']) && $_POST['rojmelId'] > 0)
    {
      if($_POST['crdr'] == "Cr")
        $field = "crAmount";
      else
        $field = "drAmount";
      
      // first all party bydefault id 0 set start
    	$updateAllDefaultParty = "UPDATE rojmel 
    	                             SET vepaariPartyId       = '0',
                                       kaarigarPartyId      = '0',
                                       bullionPartyId       = '0',
                                       bhatthiPartyRojmelId = '0'
                                 WHERE rojmelId = ".$_POST['rojmelId'];
      $updateAllDefaultPartyRes = mysql_query($updateAllDefaultParty);
      // first all party bydefault id 0 set End
      
      //Rojmel Updte : Starts
      $updateRojmel = "UPDATE rojmel
                           SET rojmelDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',bhatthiPartyRojmelId = ".$partyId.",
                               partyName = '".$partyName."',
                               particulars = '".$particulars."',fine = ".$fine.",amount = ".$amount.",CrDr = '".$_POST['crdr']."'
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $updateRojmelRes = mysql_query($updateRojmel);
      //Rojmel Updte : Ends
      
       //Selection of Main Id : Starts
      $selectIdQuery = "SELECT bhatthiBookId
                          FROM bhatthibookdetail
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $selectIdQueryRes = mysql_query($selectIdQuery);
      while($kaarigarRow = mysql_fetch_assoc($selectIdQueryRes)){
       $requiredBhattiId = $kaarigarRow['bhatthiBookId'];}                   
      //Selection of Main Id : Ends

      //BhattiBook Update : Starts
      $updateBhatthi = "UPDATE bhatthibook 
                           SET bhatthiPartyId = ".$partyId.", bhatthiDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."'
                         WHERE bhatthiBookId = ".$requiredBhattiId;
      $updateBhatthiRes = mysql_query($updateBhatthi);
      //BhattiBook Update : Ends
      ////////////////////////////////
       /// Update Vepaari Detail : Starts
      $updateRojmelId = "SELECT rojmelId
                           FROM bhatthibookdetail 
                          WHERE bhatthiBookId = ".$requiredBhattiId;
      $updateRojmelIdRes = mysql_query($updateRojmelId);    
	    if($idRow = mysql_fetch_assoc($updateRojmelIdRes))
	    {
	      $updateBhatthiDetail = "UPDATE bhatthibookdetail 
                                   SET particulars = '".$particulars."', fine = ".$fine.", amount = ".$amount.", CrDr = '".$_POST['crdr']."'
                                 WHERE rojmelId = ".$idRow['rojmelId'];
        $updateBhatthiDetailRes = mysql_query($updateBhatthiDetail);
	    }
      header("Location:rojmel.php");
    }
    else
    {
      if($_POST['crdr'] == "Cr")
        $field = "crAmount";
      else
        $field = "drAmount";
      
      $insertRoamel = "INSERT INTO rojmel(rojmelDate,vepaariPartyId,kaarigarPartyId,bullionPartyId,bhatthiPartyRojmelId,partyName,particulars, fine, amount, CrDr)
                       VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',0,0,0,".$partyId.",'".$partyName."','".$_POST['particulars']."',
                             ".$fine.",".$amount.",'".$_POST['crdr']."')";
      $insertRoamelRes = mysql_query($insertRoamel);
      $requiredRojmelId = mysql_insert_id();
      
      $insertBhatthi = "INSERT INTO bhatthibook (bhatthiPartyId, bhatthiDate)
                        VALUES (".$partyId.",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."')";
      $insertBhatthiRes = mysql_query($insertBhatthi);
      $bhatthiId = mysql_insert_id();
 
      $insertBhatthiDetail = "INSERT INTO bhatthibookdetail(bhatthiBookId, particulars, fine, amount, CrDr, rojmelId)
                              VALUES (".$bhatthiId.",'".$_POST['particulars']."',".$fine.",".$amount.",'".$_POST['crdr']."',".$requiredRojmelId.")";
      $insertBhatthiDetailRes = mysql_query($insertBhatthiDetail);
      header("Location:rojmel.php");
    }
  }////////////// Bhatthi Party Book Update and Insert Work : Ends////////////////////
  else if($_POST['bullionParty'] > 0)////////////// Bullion Party Book Update and Insert Work : Starts////////////////////
  {
    if(isset($_POST['rojmelId']) && $_POST['rojmelId'] > 0)
    {
      if($_POST['crdr'] == "Cr")
      {
        $field = "crAmount";
        $fieldFine = "crFine";
        $oppFieldFine = "kg";
        $oppField = "drAmount";
      }
      else
      {
        $field = "drAmount";
        $fieldFine = "kg";
        $oppFieldFine = "crFine";
        $oppField = "crAmount";
      }
      
      // first all party bydefault id 0 set start
    	$updateAllDefaultParty = "UPDATE rojmel 
    	                             SET vepaariPartyId       = '0',
                                       kaarigarPartyId      = '0',
                                       bullionPartyId       = '0',
                                       bhatthiPartyRojmelId = '0'
                                 WHERE rojmelId = ".$_POST['rojmelId'];
      $updateAllDefaultPartyRes = mysql_query($updateAllDefaultParty);
      // first all party bydefault id 0 set End
      
      //Rojmel Updte : Starts
      $updateRojmel = "UPDATE rojmel
                           SET rojmelDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',bullionPartyId = ".$partyId.",
                               partyName = '".$partyName."',
                               particulars = '".$particulars."',fine = ".$fine.",amount = ".$amount.",CrDr = '".$_POST['crdr']."'
                         WHERE rojmelId = ".$_POST['rojmelId'];
      $updateRojmelRes = mysql_query($updateRojmel);
      //Rojmel Updte : Ends
      
       //Sellection of Main Id : Starts
      $selectionIdQuery = "SELECT bullionmasterid
                             FROM bullionmaster
                            WHERE rojmelId = ".$_POST['rojmelId'];
      $selectionIdQueryRes = mysql_query($selectionIdQuery);
      while($bullionRow = mysql_fetch_assoc($selectionIdQueryRes)){
       $requiredBullionId = $bullionRow['bullionmasterid'];}                   
      //Sellection of Main Id : Ends

      //BullionMaster Update : Starts
      $updateBullionMaster = "UPDATE bullionmaster
                                 SET partyId = ".$partyId.",
                                     bulliondate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."'
                               WHERE rojmelId = ".$_POST['rojmelId'];
      $updateBullionMasterRes = mysql_query($updateBullionMaster);                          
      //BullionMaster Update : Ends
      
      //BullionDetail Update : Starts
      $updateBullionDetail = "UPDATE bulliondetail 
                                 SET particulars = '".$particulars."',".$fieldFine." = ".$fine.",".$field." = ".$amount.",".$oppFieldFine." = 0,".$oppField." = 0
                               WHERE bullionmasterid = ".$requiredBullionId;
      $updateBullionDetailRes = mysql_query($updateBullionDetail);
      header("Location:rojmel.php");
      //BullionDetail Update : Starts 
    }
    else
    {
      $fine   = ($_POST['fine'] != "") ? $_POST['fine'] : 0;
      $amount = ($_POST['amount'] != "") ? $_POST['amount'] : 0;	
      $particulars = ($_POST['particulars'] != '') ? $_POST['particulars'] : 0;
      
      $insertRoamel = "INSERT INTO rojmel(rojmelDate,vepaariPartyId,kaarigarPartyId,bullionPartyId,bhatthiPartyRojmelId,partyName, particulars, fine, amount, CrDr)
                    VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',0,0,".$partyId.",0,'".$partyName."','".$particulars."',
                           ".$fine.",".$amount.",'".$_POST['crdr']."')";
      $insertRoamelRes = mysql_query($insertRoamel);
      $requiredRojmelId = mysql_insert_id();
      
    	// master Record Insert Into Bullion : Start 
      $insertBullion = "INSERT INTO bullionmaster (partyId,bullionDate,rojmelId)
                        VALUES (".$partyId.",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."','".$requiredRojmelId."')";
      $insertBullionRes = mysql_query($insertBullion);
      $bullionId = mysql_insert_id();
      // master Record Insert Into Bullion : End
      // master Record Insert Into Bullion Detail : Start 
      if($_POST['crdr'] == "Cr")
      {
      	$particulars   = $_POST['particulars']   == "" ? 0 : $_POST['particulars'];
      	$creditFine    = $_POST['fine']   == "" ? 0 : $_POST['fine'];
        $creditAmount  = $_POST['amount'] == "" ? 0 : $_POST['amount'];
        $billBookDetail = "INSERT INTO bulliondetail (bullionmasterid,particulars,crfine,cramount,inRojmel)
                           VALUES (".$bullionId.",'".$particulars."',".$creditFine.",".$creditAmount.",1)";
        $billBookDetailRes = mysql_query($billBookDetail);
        header("Location:rojmel.php");
      }
      else
      {
      	$debitAmount   = $_POST['amount'] == "" ? 0 : $_POST['amount'];
      	$debitFine     = $_POST['fine'] == "" ? 0 : $_POST['fine'];
      	$particulars   = $_POST['particulars']   == "" ? 0 : $_POST['particulars'];
        $billBookDetail = "INSERT INTO bulliondetail (bullionmasterid,particulars,kg,dramount,inRojmel)
                           VALUES (".$bullionId.",'".$particulars."',".$debitFine.",".$debitAmount." ,1)";
        $billBookDetailRes = mysql_query($billBookDetail);
        header("Location:rojmel.php");
      }
    }
    // master Record Insert Into Bullion Detail : Ends
  }////////////// Bullion Party Book Update and Insert Work : Ends////////////////////
}

// This for select rojmel record: Start
if(isset($_GET['rojmelId']) && $_GET['rojmelId'] > 0)
{
  $selectRojmel = "SELECT rojmelId,rojmelDate,vepaariPartyId,particulars,fine,amount,CrDr FROM rojmel
                    WHERE rojmelId = ".$_GET['rojmelId'];
  $selectRojmelRes = mysql_query($selectRojmel);
  if($rojmelRow = mysql_fetch_assoc($selectRojmelRes))
  {
    $rojmel['id']             = $rojmelRow['rojmelId'];
    $rojmel['table']          = "rojmel";
    $rojmel['date']           = $rojmelRow['rojmelDate'];
    $rojmel['vepaariPartyId'] = $rojmelRow['vepaariPartyId'];
    $rojmel['particulars']    = $rojmelRow['particulars'];
    $rojmel['fine']           = $rojmelRow['fine'];
    $rojmel['amount']         = $rojmelRow['amount'];
    $rojmel['CrDr']           = $rojmelRow['CrDr'];
  }
}
// This for select rojmel record : End

// This for select bhatthi record : Start
if(isset($_GET['bhatthiId']) && $_GET['bhatthiId'] > 0)
{
  $selectBhatthi = "SELECT bhatthibook.bhatthiBookId, DATE_FORMAT(bhatthibook.bhatthiDate,'%Y-%m-%d') As bhatthiDate, bhatthibook.bhatthiPartyId ,
                           bhatthibookdetail.particulars, bhatthibookdetail.fine, bhatthibookdetail.amount, bhatthibookdetail.CrDr
                      FROM bhatthibook
                      JOIN bhatthibookdetail ON bhatthibookdetail.bhatthiBookId = bhatthibook.bhatthiBookId
                     WHERE bhatthibook.bhatthiBookId = ".$_GET['bhatthiId'];
  $selectBhatthiRes = mysql_query($selectBhatthi);
  if($bhatthiRow = mysql_fetch_assoc($selectBhatthiRes))
  {
    $rojmel['id']             = $bhatthiRow['bhatthiBookId'];
    $rojmel['table']          = "bhatthiBook";
    $rojmel['bhatthiPartyId'] = $bhatthiRow['bhatthiPartyId'];
    $rojmel['date']           = $bhatthiRow['bhatthiDate'];
    $rojmel['particulars']    = $bhatthiRow['particulars'];
    $rojmel['fine']           = $bhatthiRow['fine'];
    $rojmel['amount']         = $bhatthiRow['amount'];
    $rojmel['CrDr']           = $bhatthiRow['CrDr'];
  }
}
// This for select bhatthi record : End

// This for select kaarigar record : Start
if(isset($_GET['kaarigardetailId']) && $_GET['kaarigardetailId'] > 0)
{
  $selectKaarigar = "SELECT kaarigardetailId, kaarigarId, DATE_FORMAT(kaarigarDate,'%Y-%m-%d') AS kaarigarDate,particulars, crAmount, drAmount
                       FROM kaarigardetail
                      WHERE kaarigardetailId = ".$_GET['kaarigardetailId'];
  $selectKaarigarRes = mysql_query($selectKaarigar);
  if($kaarigarRow = mysql_fetch_assoc($selectKaarigarRes))
  {
    $rojmel['id']          = $kaarigarRow['kaarigardetailId'];
    $rojmel['table']       = "kaarigarDetail";
    $rojmel['kaarigarId']  = $kaarigarRow['kaarigarId'];
    $rojmel['date']        = $kaarigarRow['kaarigarDate'];
    $rojmel['particulars'] = $kaarigarRow['particulars'];
    if($kaarigarRow['drAmount'] != '')
    {
      $rojmel['amount'] = $kaarigarRow['drAmount'];
      $rojmel['CrDr']   = "Dr";
    }
    else
    {
      $rojmel['amount'] = $kaarigarRow['crAmount'];
      $rojmel['CrDr']   = "Cr";
    }
  }
}
// This for select kaarigar record : End

// This for select Vepaari record : Start
if(isset($_GET['vepaariBookId']) && $_GET['vepaariBookId'] > 0)
{
  $selectVepaari = "SELECT vepaaribookId, partyId, vepaariBookDate, particulars, fine, amount, CrDr
                      FROM vepaaribook
                     WHERE vepaaribookId = ".$_GET['vepaariBookId'];
  $selectVepaariRes = mysql_query($selectVepaari);
  if($vepaariRow = mysql_fetch_assoc($selectVepaariRes))
  {
    $rojmel['id']          = $vepaariRow['vepaaribookId'];
    $rojmel['table']       = "vepaariBook";
    $rojmel['partyId']     = $vepaariRow['partyId'];
    $rojmel['date']        = $vepaariRow['vepaariBookDate'];
    $rojmel['particulars'] = $vepaariRow['particulars'];
    $rojmel['fine']        = $vepaariRow['fine'];
    $rojmel['amount']      = $vepaariRow['amount'];
    $rojmel['CrDr']        = $vepaariRow['CrDr'];
  }
}
// This for select Vepaari record : Ends
// This for select party : Start
$a = 0;
$party['partyId'][$a] = 0;
$party['partyName'][$a] = "Select";
$a++;
$selectParty = "SELECT partyId,partyName
                  FROM party
                 ORDER BY partyName DESC";
$selectPartyRes = mysql_query($selectParty);

while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]   = $partyRow['partyId'] . '_' . $partyRow['partyName'];
  $party['partyName'][$a] = $partyRow['partyName'];
  $a++;
}
// This for select party : End

// This for select kaarigar : Start
$selectKaarigar = "SELECT kaarigarId,kaarigarName
                     FROM kaarigar
                    ORDER BY kaarigarName DESC";
$selectKaarigarRes = mysql_query($selectKaarigar);
$a = 0;
$kaarigar['id'][$a] = 0;
$kaarigar['name'][$a] = "Select";
$a++;
while($kaarigarRow = mysql_fetch_assoc($selectKaarigarRes))
{
  $kaarigar['id'][$a] = $kaarigarRow['kaarigarId'] . '_' . $kaarigarRow['kaarigarName'];
  $kaarigar['name'][$a] = $kaarigarRow['kaarigarName'];
  $a++;
}
// This for select kaarigar : End

// This for select Bullion record : End
$bullionPartyName ="";
// This for select Bullion party : Start
$b = 0;
$partyBullion['partyId'][$b]   = 0;
$partyBullion['bullionPartyName'][$b] = "Select";
$b++;
$selectBullionParty = "SELECT partyId,bullionPartyName
                         FROM bullionparty
                     ORDER BY bullionPartyName DESC";
$selectBullionPartyRes = mysql_query($selectBullionParty);
while($partyBullionRow = mysql_fetch_assoc($selectBullionPartyRes))
{
  $partyBullion['partyId'][$b]          = $partyBullionRow['partyId']. '_' . $partyBullionRow['bullionPartyName'];
  $partyBullion['bullionPartyName'][$b] = $partyBullionRow['bullionPartyName'];
  $b++;
}
// This for select Bullion party : End
// This for select bhatthi party : Start
$selectBhatthiParty = "SELECT bhatthiPartyId,bhatthipartyName
                         FROM bhatthiparty
                        ORDER BY bhatthipartyName DESC";
$selectBhatthiPartyRes = mysql_query($selectBhatthiParty);
$a = 0;
$bhatthiParty['id'][$a]   = 0;
$bhatthiParty['name'][$a] = "Select";
$a++;
while($bhatthiPartyRow = mysql_fetch_assoc($selectBhatthiPartyRes))
{
  $bhatthiParty['id'][$a]   = $bhatthiPartyRow['bhatthiPartyId']. '_' . $bhatthiPartyRow['bhatthipartyName'];
  $bhatthiParty['name'][$a] = $bhatthiPartyRow['bhatthipartyName'];
  $a++;
}
// this for select bhatthi party : End

if(isset($_GET['rojmelId']) && $_GET['rojmelId'] > 0)
{ 
	//Edit Party In Rojmel:Start
	$selectRojmelParty = " SELECT rojmelId,vepaariPartyId,party.partyId,party.partyName
	                         FROM rojmel
	                         JOIN party
	                        WHERE rojmelId = ".$_GET['rojmelId']."
	                          AND vepaariPartyId = party.partyId";
	$selectRojmelPartyRes = mysql_query($selectRojmelParty);
	if($row = mysql_fetch_assoc($selectRojmelPartyRes))
	{
		if($row['vepaariPartyId'] > 0)
		  $vepaariPartyId = $row['vepaariPartyId']."_".$row['partyName'];  
	}
	$selectRojmelParty = " SELECT rojmelId,rojmel.kaarigarPartyId,kaarigar.kaarigarId,kaarigar.kaarigarName
	                         FROM rojmel
	                         JOIN kaarigar
	                        WHERE rojmelId = ".$_GET['rojmelId']."
	                          AND rojmel.kaarigarPartyId = kaarigar.kaarigarId";
	$selectRojmelPartyRes = mysql_query($selectRojmelParty);
	if($row = mysql_fetch_assoc($selectRojmelPartyRes))
	{
		if($row['kaarigarPartyId'] > 0)
		  $kaarigarPartyId = $row['kaarigarPartyId']."_".$row['kaarigarName'];  
	}
	$selectRojmelParty = " SELECT rojmelId,rojmel.bullionPartyId,bullionparty.partyId,bullionparty.bullionPartyName
	                         FROM rojmel
	                         JOIN bullionparty
	                        WHERE rojmelId = ".$_GET['rojmelId']."
	                          AND rojmel.bullionPartyId = bullionparty.partyId";
	$selectRojmelPartyRes = mysql_query($selectRojmelParty);
	if($row = mysql_fetch_assoc($selectRojmelPartyRes))
	{
		if($row['bullionPartyId'] > 0)
		  $bullionPartyId = $row['bullionPartyId']."_".$row['bullionPartyName'];  
	}
	$selectRojmelParty = " SELECT rojmelId,bhatthiPartyRojmelId,bhatthiparty.bhatthiPartyId,bhatthiparty.bhatthipartyName
	                         FROM rojmel
	                         JOIN bhatthiparty	 
	                        WHERE rojmelId = ".$_GET['rojmelId']."
	                          AND bhatthiPartyRojmelId	= bhatthiparty.bhatthiPartyId";
	$selectRojmelPartyRes = mysql_query($selectRojmelParty);
	if($row = mysql_fetch_assoc($selectRojmelPartyRes))
	{
		if($row['bhatthiPartyRojmelId'] > 0)
		  $bhatthiPartyRojmelId	= $row['bhatthiPartyRojmelId']."_".$row['bhatthipartyName'];  
	}
}
//Edit Party In Rojmel:End
$smarty = new Smarty;
require('bottom.php');
$smarty->assign("l_date",$l_date);
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_cancel",$l_cancel);
$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_kaarigarName",$l_kaarigarName);
$smarty->assign("l_bhatthiParty",$l_bhatthiParty);
$smarty->assign("l_bullionpartySelect",$l_bullionpartySelect);
$smarty->assign("l_save",$l_save);

$smarty->assign("vepaariPartyId",$vepaariPartyId);
$smarty->assign("kaarigarPartyId",$kaarigarPartyId);
$smarty->assign("bullionPartyId",$bullionPartyId);
$smarty->assign("bhatthiPartyRojmelId",$bhatthiPartyRojmelId);
$smarty->assign("requiredVepaariId",$requiredVepaariId);
$smarty->assign("party",$party);
$smarty->assign("partyBullion",$partyBullion);
$smarty->assign("kaarigar",$kaarigar);
$smarty->assign("bhatthiParty",$bhatthiParty);
$smarty->assign("rojmel",$rojmel);
$smarty->assign("bullionPartyName",$bullionPartyName);
$smarty->assign("rojmelId",$rojmelId);
$smarty->assign("totalFine",$totalFine);
$smarty->assign("totalAmount",$totalAmount);
$smarty->assign("showPrev",$showPrev);
$smarty->assign("vepaariBookMasterId",$vepaariBookMasterId);
$smarty->display('rojmelEntry.tpl');
?>